﻿using Common;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Serialization;
using System;

namespace CarPCBATest.Procedure
{
    [XmlRoot("控制板配置")]
    [Serializable]
    public class AllPcbConfig : NotifyBase
    {
        [XmlElement("工装只能测试一套")]
        public bool IsOneProduct { get; set; }

        [XmlElement("标题")]
        public string Title { get; set; }

        [XmlElement("电流表串口")]
        public string DM3025EPort { get; set; }

        [XmlElement("左SN1验证码")]
        public string SN1code_L { get; set; }

        [XmlElement("左SN2验证码")]
        public string SN2code_L { get; set; }

        [XmlElement("左SN3验证码")]
        public string SN3code_L { get; set; }

        [XmlElement("左SN4验证码")]
        public string SN4code_L { get; set; }

        [XmlElement("右SN1验证码")]
        public string SN1code_R { get; set; }

        [XmlElement("右SN2验证码")]
        public string SN2code_R { get; set; }

        [XmlElement("右SN3验证码")]
        public string SN3code_R { get; set; }

        [XmlElement("右SN4验证码")]
        public string SN4code_R { get; set; }

        [XmlElement("Jlink烧录")]
        public string JlinkSL { get; set; }

        [XmlElement("Jlink软件")]
        public string JlinkEXE { get; set; }

        [XmlElement("开断路串口")]
        public string zhiConPort { get; set; }

        [XmlElement("IO卡")]
        public string IOModule1IP { get; set; }

        [XmlElement("电压表串口")]
        public string IT6302APort { get; set; }

        private string _webServiceUrl;

        [XmlElement("WebService地址")]
        public string WebServiceUrl
        {
            get { return _webServiceUrl; }
            set
            {
                _webServiceUrl = value;
                this.DoNotify();
            }
        }

        private string _callMethodName;

        [XmlElement("调用方法名")]
        public string CallMethodName
        {
            get { return _callMethodName; }
            set
            {
                _callMethodName = value;
                this.DoNotify();
            }
        }

        private string _station;

        [XmlElement("站号")]
        public string Station
        {
            get { return _station; }
            set
            {
                _station = value;
                this.DoNotify();
            }
        }

        private bool _uploadMes;

        [XmlElement("上传MES开关")]
        public bool UploadMes
        {
            get { return _uploadMes; }
            set
            {
                _uploadMes = value;
                this.DoNotify();
            }
        }

        public int GetNewProjNo()
        {
            if (ListConfig is null)
            {
                return 0;
            }

            int minNo = ListConfig.Where(t => t.No >= 0).Select(t => t.No).OrderBy(n => n).FirstOrDefault();

            if (minNo == 0)
            {
                int maxNo = ListConfig.Max(t => t.No);
                return Enumerable.Range(minNo, maxNo + 2).Except(ListConfig.Select(t => t.No)).FirstOrDefault();
            }

            return 0;
        }

        private int _usingNo;

        [XmlAttribute("启用方案编号")]
        public int UsingNo
        {
            get { return _usingNo; }
            set
            {
                _usingNo = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<PcbConfig> _listConfig = new ObservableCollection<PcbConfig>();

        [XmlElement("测试方案")]
        public ObservableCollection<PcbConfig> ListConfig
        {
            get { return _listConfig; }
            set
            {
                _listConfig = value;
                this.DoNotify();
            }
        }

        public AllPcbConfig()
        {
            ListConfig.CollectionChanged += ListConfig_CollectionChanged;
        }

        private void ListConfig_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ListConfig = sender as ObservableCollection<PcbConfig>;
        }
    }

    /// <summary>
    /// 控制板配置
    /// </summary>
    [Serializable]
    public class PcbConfig : NotifyBase
    {
        private int _no;

        [XmlAttribute("编号")]
        public int No
        {
            get { return _no; }
            set
            {
                _no = value;
                this.DoNotify();
            }
        }

        private string _name;

        [XmlAttribute("名称")]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                this.DoNotify();
            }
        }

        private string _visionProjPath;

        [XmlAttribute("视觉方案路径")]
        public string VisionProjPath
        {
            get { return _visionProjPath; }
            set
            {
                _visionProjPath = value;
                this.DoNotify();
            }
        }

        private int _probeUsedTimes;

        [XmlAttribute("探针使用次数")]
        public int ProbeUsedTimes
        {
            get { return _probeUsedTimes; }
            set
            {
                _probeUsedTimes = value;
                this.DoNotify();
            }
        }

        private int _probeMaxUsedTimes;

        [XmlAttribute("探针报警次数")]
        public int ProbeMaxUsedTimes
        {
            get { return _probeMaxUsedTimes; }
            set
            {
                _probeMaxUsedTimes = value;
                this.DoNotify();
            }
        }

        private int _snLength;

        [XmlAttribute("SN码长度")]
        public int SNLength
        {
            get { return _snLength; }
            set
            {
                _snLength = value;
                this.DoNotify();
            }
        }

        private string _chipType;

        [XmlAttribute("芯片型号")]
        public string ChipType
        {
            get { return _chipType; }
            set
            {
                _chipType = value;
                this.DoNotify();
            }
        }

        private bool _snCheck;

        [XmlAttribute("SN规则验证")]
        public bool SNCheck
        {
            get { return _snCheck; }
            set
            {
                _snCheck = value;
                this.DoNotify();
            }
        }

        private string _ctlStyle;

        [XmlAttribute("点灯控制方式")]
        public string CtlStyle
        {
            get { return _ctlStyle; }
            set
            {
                _ctlStyle = value;
                this.DoNotify();
            }
        }

        private BurnConfig _burnConfig;

        [XmlElement("烧录配置")]
        public BurnConfig BurnConfig
        {
            get { return _burnConfig; }
            set
            {
                _burnConfig = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<PowerConfig> _listPowerConfig = new ObservableCollection<PowerConfig>();

        [XmlElement("电源配置")]
        public ObservableCollection<PowerConfig> ListPowerConfig
        {
            get { return _listPowerConfig; }
            set
            {
                _listPowerConfig = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<LedCurrentConfig> _listLedCurrentConfig = new ObservableCollection<LedCurrentConfig>();

        [XmlElement("功能测试配置")]
        public ObservableCollection<LedCurrentConfig> ListLedCurrentConfig
        {
            get { return _listLedCurrentConfig; }
            set
            {
                _listLedCurrentConfig = value;
                this.DoNotify();
            }
        }

        public PcbConfig()
        {
            ListLedCurrentConfig.CollectionChanged += ListLedCurrentConfig_CollectionChanged;
            ListPowerConfig.CollectionChanged += ListPowerConfig_CollectionChanged;
        }

        private void ListPowerConfig_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ListPowerConfig = sender as ObservableCollection<PowerConfig>;
        }

        private void ListLedCurrentConfig_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ListLedCurrentConfig = sender as ObservableCollection<LedCurrentConfig>;
        }
    }

    /// <summary>
    /// 芯片配置
    /// </summary>
    [Serializable]
    public class SubBurnConfig : NotifyBase
    {
        private int _mcuAddress;

        [XmlAttribute("芯片地址")]
        public int McuAddress
        {
            get { return _mcuAddress; }
            set
            {
                _mcuAddress = value;
                this.DoNotify();
            }
        }

        private string _brunFilePath;

        [XmlAttribute("烧录文件路径")]
        public string BrunFilePath
        {
            get { return _brunFilePath; }
            set
            {
                _brunFilePath = value;
                this.DoNotify();
            }
        }

        private bool _isBurn;

        [XmlAttribute("是否烧录")]
        public bool IsBurn
        {
            get { return _isBurn; }
            set
            {
                _isBurn = value;
                this.DoNotify();
            }
        }
    }

    /// <summary>
    /// 烧录配置
    /// </summary>
    [Serializable]
    public class BurnConfig : NotifyBase
    {
        public BurnConfig()
        {
            SubBurnConfigs = new ObservableCollection<SubBurnConfig>();
        }

        private string _port;

        [XmlAttribute("串口")]
        public string Port
        {
            get { return _port; }
            set
            {
                _port = value;
                this.DoNotify();
            }
        }

        private int _paud;

        [XmlAttribute("波特率")]
        public int Paud
        {
            get { return _paud; }
            set
            {
                _paud = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<SubBurnConfig> subBurnConfigs;

        [XmlElement("芯片配置")]
        public ObservableCollection<SubBurnConfig> SubBurnConfigs
        {
            get { return subBurnConfigs; }
            set
            {
                subBurnConfigs = value;
                this.DoNotify();
            }
        }
    }

    /// <summary>
    /// 电流测试配置
    /// </summary>
    [Serializable]
    public class LedCurrentConfig : NotifyBase
    {
        private string _ledDesc;

        [XmlAttribute("功能")]
        public string LedDesc
        {
            get { return _ledDesc; }
            set
            {
                _ledDesc = value;
                this.DoNotify();
            }
        }

        private int _stationCount;

        [XmlAttribute("工位数量")]
        public int StationCount
        {
            get { return _stationCount; }
            set
            {
                _stationCount = value;
                this.DoNotify();
            }
        }

        private double _testVoltage;

        [XmlAttribute("测试电压")]
        public double TestVoltage
        {
            get { return _testVoltage; }
            set
            {
                _testVoltage = value;
                this.DoNotify();
            }
        }

        private double _testCurrent;

        [XmlAttribute("测试电流")]
        public double TestCurrent
        {
            get { return _testCurrent; }
            set
            {
                _testCurrent = value;
                this.DoNotify();
            }
        }

        private double _upperLimitCurrent;

        [XmlAttribute("电流上限")]
        public double UpperLimitCurrent
        {
            get { return _upperLimitCurrent; }
            set
            {
                _upperLimitCurrent = value;
                this.DoNotify();
            }
        }

        private double _lowerLimitCurrent;

        [XmlAttribute("电流下限")]
        public double LowerLimitCurrent
        {
            get { return _lowerLimitCurrent; }
            set
            {
                _lowerLimitCurrent = value;
                this.DoNotify();
            }
        }

        private string _procName;

        [XmlAttribute("视觉流程名称")]
        public string ProcName
        {
            get { return _procName; }
            set
            {
                _procName = value;
                this.DoNotify();
            }
        }

        private uint _procId;

        [XmlAttribute("视觉流程ID")]
        public uint ProcID
        {
            get { return _procId; }
            set
            {
                _procId = value;
                this.DoNotify();
            }
        }

        private bool _waterFailLight;

        [XmlAttribute("受否流水灯")]
        public bool WaterFailLight
        {
            get { return _waterFailLight; }
            set
            {
                _waterFailLight = value;
                this.DoNotify();
            }
        }

        private int _waterFailTime;

        [XmlAttribute("流水时常")]
        public int WaterFailTime
        {
            get { return _waterFailTime; }
            set
            {
                _waterFailTime = value;
                this.DoNotify();
            }
        }

        private string _testMethodName;

        [XmlAttribute("测试调用方法名称")]
        public string TestMethodName
        {
            get { return _testMethodName; }
            set
            {
                _testMethodName = value;
                this.DoNotify();
            }
        }
    }

    /// <summary>
    /// 电源配置
    /// </summary>
    [Serializable]
    public class PowerConfig : NotifyBase
    {
        private int _channelIndex;

        [XmlAttribute("电源通道索引")]
        public int ChannelIndex
        {
            get { return _channelIndex; }
            set
            {
                _channelIndex = value;
                this.DoNotify();
            }
        }

        private bool _isActive;

        [XmlAttribute("是否启用")]
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                this.DoNotify();
            }
        }

        private double _voltage;

        [XmlAttribute("输出电压")]
        public double Voltage
        {
            get { return _voltage; }
            set
            {
                _voltage = value;
                this.DoNotify();
            }
        }

        private double _current;

        [XmlAttribute("输出电流")]
        public double Current
        {
            get { return _current; }
            set
            {
                _current = value;
                this.DoNotify();
            }
        }
    }
}